package com.ugrow.ugrowcommunity.activity.courseevaluation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ugrow.ugrowcommunity.BuildConfig;
import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.courseevaluation.adapter.EvaluationAdapter;
import com.ugrow.ugrowcommunity.activity.courseevaluation.bean.CourseBean;
import com.ugrow.ugrowcommunity.activity.courseevaluation.bean.Data;
import com.ugrow.ugrowcommunity.activity.courseevaluation.bean.EvaluationBean;
import com.ugrow.ugrowcommunity.databinding.ActivityCourseEvaluationBinding;
import com.ugrow.ugrowcommunity.util.HttpUtil;
import com.ugrow.ugrowcommunity.vm.mine.MineFragment;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CourseEvaluationActivity extends AppCompatActivity {
    private ActivityCourseEvaluationBinding binding;
    private List<Data> dataList;
    private Data data;
    private boolean isClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCourseEvaluationBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        back();
        showTeacherInformation();
    }

    //点击返回
    private void back() {
        binding.evaluationBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //点击查询显示教师信息
    private void showTeacherInformation() {
        binding.evaluationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.evaluationId.getText().toString().length() != 0) {
                    getData();
                } else {
                    Toast.makeText(CourseEvaluationActivity.this, "请输入学号进行查询", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    //请求接口信息
    private void getData() {
//        OkHttpClient okHttpClient = new OkHttpClient();
//        FormBody formBody = new FormBody.Builder().build();
//        Request request = new Request.Builder().post(formBody).url(BuildConfig.EVALUATION_TEACHER).build();
//        Call call = okHttpClient.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call, @NotNull IOException e) {
//
//            }
//
//            @Override
//            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                String string = response.body().string();
//
//            }
//        });
        HashMap<String, Object> map = new HashMap<>();
        String path = "http://82.157.173.219:8080/SmartCampus/teacher/selectTeachers";
        HttpUtil.httpPost(path, map, HttpUtil.ContentType.JSON, new HttpUtil.CallBack() {
            @Override
            public void success(HttpUtil.Result result) {
                EvaluationBean evaluationBean = new Gson().fromJson(result.content, EvaluationBean.class);
                //Log.d("TAG", "success: "+result.content);
                if (evaluationBean.getCode() == 200) {
                    dataList = evaluationBean.getData();
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CourseEvaluationActivity.this, RecyclerView.VERTICAL, false);
                    binding.recyclerview.setLayoutManager(linearLayoutManager);

                    EvaluationAdapter adapter = new EvaluationAdapter(CourseEvaluationActivity.this, dataList);
                    adapter.setOnItemClickListener(new EvaluationAdapter.OnItemClickListener() {
                        @Override
                        public void onClickListener(View view, int position) {
                            //data.setClick(false);
                            Data data = dataList.get(position);
                            CourseBean course = data.getCourse();
                            String tchName = data.getTchName();
                            String tchCourse ;
                            String stucourse = null;
                            if (course == null) {
                                tchCourse = "Android开发";
                                stucourse = "孙武";
                            }else {
                                tchCourse = course.getcNature();
                                stucourse = course.getcName();
                            }
                            Intent intent = new Intent();
                            intent.putExtra("name",tchName);
                            intent.putExtra("course",tchCourse);
                            intent.putExtra("stucourse",stucourse);
                            intent.setClass(CourseEvaluationActivity.this,EvaluationActivity.class);
                            startActivity(intent);
                        }
                    });


                    binding.recyclerview.setAdapter(adapter);

                }
            }

            @Override
            public void failed(IOException e) {

            }
        });
    }
}